package com.shouer.doc.ui.ordermanager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shouer.doc.R;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.ui.neworder.model.HealthRecord;
import com.shouer.doc.ui.ordermanager.presenter.IOrderManagerPresenter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY on 2017/8/8.
 */

public class HealthRecordAdapter extends RecyclerView.Adapter<HealthRecordVH> implements View.OnClickListener{

    private Context mContext;
    private IOrderManagerPresenter managerPresenter;
    private LayoutInflater mInflater;
    private List<HealthRecord> mHealthRecords;

    public HealthRecordAdapter(Context context,IOrderManagerPresenter managerPresenter)
    {
        this.mContext=context;
        this.managerPresenter=managerPresenter;
        mInflater=LayoutInflater.from(context);

    }

    @Override
    public HealthRecordVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView=mInflater.inflate(R.layout.fragment_ordermanager_healthrecord_item_layout,null);
        return new HealthRecordVH(itemView);
    }

    @Override
    public void onBindViewHolder(HealthRecordVH holder, int position) {


        HealthRecord record=mHealthRecords.get(position);

        holder.titleView.setText(TextUtils.isEmpty(record.getItemName())?"":record.getItemName());

        try {
            Date createDate= DateFormatUtil.stringToDate(record.getUpdateDate().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
            holder.dateView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd"));
        } catch (Exception e) {
            Log.d(HealthRecordAdapter.class.getSimpleName(), e.toString());
            holder.dateView.setText("");
        }

        holder.patName.setText(TextUtils.isEmpty(record.getPatient().getPatName())?"":record.getPatient().getPatName());
        holder.patSex.setText(record.getPatient().getPatSex()==0?"女":"男");

        try {
            Date createDate=DateFormatUtil.stringToDate(record.getPatient().getPatBirthday().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
            holder.patBirDate.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd"));
        } catch (Exception e) {
            Log.d(HealthRecordAdapter.class.getSimpleName(), e.toString());
            holder.patBirDate.setText("");
        }

        holder.itemView.setTag(R.id.list_position,position);
        holder.itemView.setOnClickListener(this);

    }

    public void refreshHealthRecordList(List<HealthRecord> healthRecords) {
        this.mHealthRecords = healthRecords;
        notifyDataSetChanged();
    }

    public void attachToHealthRecordList(List<HealthRecord> healthRecords) {
        if (null != healthRecords) {
            if (null == mHealthRecords) {
                mHealthRecords = new ArrayList<>(0);
            }
            mHealthRecords.addAll(healthRecords);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        if(null==mHealthRecords)
        {
            return 0;
        }else
        {
            return mHealthRecords.size();
        }
    }

    @Override
    public void onClick(View view) {

        int position= (int) view.getTag(R.id.list_position);
        HealthRecord healthRecord=getItemByPosition(position);
        if (null!=healthRecord)
        {
            managerPresenter.onHealthRecordItemClick(healthRecord);
        }
    }


    private HealthRecord getItemByPosition(int position)
    {
        try {
            return mHealthRecords.get(position);
        }catch (Exception e)
        {
            Log.e(HealthRecordAdapter.class.getSimpleName(),"getItemByPosition:"+e);
            return null;
        }

    }
}
